package com.hsxc.cdz.mgmt.station;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hsxc.cdz.base.bean.Address;
import com.hsxc.cdz.base.bean.ConsoleUser;
import com.hsxc.cdz.base.bean.Pile;
import com.hsxc.cdz.base.bean.Station;
import com.hsxc.cdz.base.dao.PileDao;
import com.hsxc.cdz.base.dao.StationDao;

@Component
public class StationManager {

	private Logger logger = LoggerFactory.getLogger(StationManager.class);
	@Autowired
	private StationDao stationDao;
	@Autowired
	private PileDao pileDao;

	// 新建充电站
	public int addStation(Station station) {
		return stationDao.insertStation(station);
	}

	// 删除一个充电站
	public int delStation(String stationNO) {
		return stationDao.deleteStation(stationNO);
	}

	// 更新充电站数据
	public int updateStation(Station station) {
		return stationDao.updateStation(station);
	}

	// 根据名称查询充电站
	public Station selStation(String name) {
		Station station = new Station();
		station.setName(name);
		return stationDao.selectStation(station);
	}

	// 根据充电站编号查询
	public Station selStationByNO(String stationNO) {
		Station station = new Station();
		station.setStationNO(stationNO);
		return stationDao.selectStation(station);
	}

	// 根据充电桩编号查询
	public Station selStationByPileNO(String pileNO) {
		Pile pile = new Pile();
		pile.setPileNO(pileNO);
		pile = pileDao.selectPile(pile);
		if (pile == null) {
			return null;
		} else {
			return pile.getStation();
		}
	}

	public String getNOByName(String stationName) {
		Station station = new Station();
		station.setName(stationName);
		Station result = stationDao.selectStation(station);
		return result.getStationNO();
	}

	/**
	 * 根据电站管理员名称查找电站编号
	 * 
	 * @param mgrName
	 */
	public List<String> getStationNOByName(String mgrName) {
		return stationDao.getStationNOByName(mgrName);

	}

	public List<Station> getCDZhanByAddress(Address address) {
		Station station = new Station();
		station.setProvince(address.getProvince());
		station.setCity(address.getCity());
		station.setCounty(address.getCounty());
		station.setStreet(address.getStreet());
		return stationDao.selectStations(station);
	}

	// 根据用户姓名查找某一个充电站
	public Station getCDZhanByName(String operatorNO) {
		Station station = new Station();
		station.setOperatorNO(operatorNO);
		return stationDao.selectStation(station);
	}

	// 根据用户姓名和角色查找所属的充电站
	public List<Station> getCDZhanByRoleName(Address address, String user_name, String operatorNo, String role) {
		// Station station = new Station();
		Station station = new Station();
		ConsoleUser consoleUser = new ConsoleUser();
		station.setProvince(address.getProvince());
		station.setCity(address.getCity());
		station.setCounty(address.getCounty());
		// station.setStreet(address.getStreet());
		consoleUser.setRole(role);
		station.setOperatorNO(operatorNo);
		station.setMgrName(user_name);
		logger.info(operatorNo);
		station.setConsoleUser(consoleUser);
		logger.info("电站查找中...");
		return stationDao.selectStations(station);
	}

	// public List<Station> getCDZhanByRoleName(int id,String role){
	// //Station station = new Station();
	// Station station = new Station();
	// ConsoleUser consoleUser = new ConsoleUser();
	// consoleUser.setRole(role);
	// consoleUser.setId(id);
	// //station.setOperatorNO(operatorNo);
	// //System.out.println(operatorNo);
	// station.setConsoleUser(consoleUser);
	// System.out.println("电站查找中...");
	// return stationDao.selectStations(station);
	// }
	public List<Map<String, Object>> getByLatlong(double[] range) {
		Map<String, Double> map = new HashMap<>();
		map.put("minLat", range[0]);
		map.put("maxLat", range[1]);
		map.put("minLong", range[2]);
		map.put("maxLong", range[3]);
		return stationDao.selectByLatLong(map);
	}

	public String getStationAddressByStationNo(String stationNo) {
		return stationDao.getStationAddressByStatioNo(stationNo);
	}
	public List<Map<String,Object>> getStationByStationName(String name){
		Station station = new Station();
		station.setName(name);
		return stationDao.getStationByName(station);
	}
}
